<?php

class goodsController extends manageController {

    public function __construct() {
        if (!isset($_SESSION['zp_manage']['islogin']) || empty($_SESSION['zp_manage']['islogin'])) {
            Tools::AlertTxt('?md=manage&cl=login&at=login', '请先登录');
            exit;
        }
    }


    public function goodsWeightAction() {
        $table = "goods_weight";
        $func = (!empty($this->params['func'])) ? $this->params['func'] : '';
        $page = (!empty($this->params['page'])) ? (int) $this->params['page'] : 1;
        $number_page = (!empty($this->params['number_page'])) ? $this->params['number_page'] : Data::$_number_page;
        $gname = (!empty($this->params['gname'])) ? $this->params['gname'] : '';
        $gmodel = (!empty($this->params['gmodel'])) ? $this->params['gmodel'] : '';
        $gnum = (!empty($this->params['gnum'])) ? $this->params['gnum'] : '';

        //上下線
        if (!empty($func) && $func == "status") {
            $id = empty($this->params["id"]) ? '0' : (int) $this->params["id"];
            if ($id > 0) {
                $where_fields = array("AND" => array("=" => array("id" => $id)));
                $row = $this->model->getItemById($id, $table);
                $field = array();
                $field['status'] = $row['status'] == '1' ? '0' : '1';
                $field['editor'] = $_SESSION['zp_manage']['uid'];
                $field['updatedt'] = 'now()';
                $r = $this->model->updateItem($field, $where_fields, $table);

                //日志
                $log = array();
                $log['table_id'] = $id;
                $log['table_name'] = $table;
                $log['type'] = 4;
                $log['page'] = "规格上下线";
                $log['content'] = $log['page'] . "：" . $this->params["id"];
                $org = array();
                $field1 = array();
                $this->model->setLog($log, $org, $field1);
            }
           echo json_encode(array('status'=>1),JSON_UNESCAPED_UNICODE);
            exit;
        }

        //刪除
        if (!empty($func) && $func == "del") {
            $id = empty($this->params["id"]) ? '0' : (int) $this->params["id"];
            $field = array("isdeleted" => 1, 'editor' => $_SESSION['zp_manage']['uid'], 'updatedt' => 'now()');
            $where_fields = array("AND" => array("=" => array("id" => $id)));
            $this->model->updateItem($field, $where_fields, $table);

            //日志
            $log = array();
            $log['table_id'] = $id;
            $log['table_name'] = $table;
            $log['type'] = 3;
            $log['page'] = "删除商品重量";
            $log['content'] = $log['page'] . "：" . $this->params["id"];
            $org = array();
            $field1 = array();
            $this->model->setLog($log, $org, $field1);
            echo json_encode(array('status'=>1),JSON_UNESCAPED_UNICODE);
            exit;
        }

        //抓列表
        $where_fields = array("AND" => array("=" => array("isdeleted" => 0)));
        if (!empty($gnum)) {
            $where_fields['AND']['=']['goods_num'] = $gnum;
        }
        $like_fields = array();
        if (!empty($gname)) {
            $like_fields['goods_name'] = $gname;
        }
        if (!empty($gmodel)) {
            $like_fields['goods_model'] = $gmodel;
        }
        $orderby = array('id' => 'desc');
        $total_count = $this->model->getTotalCount($table, $where_fields, $like_fields);
        $rsp_list = $this->model->getAllByPage($page, $number_page, $where_fields, $table, $like_fields, $orderby);
        $rsp_page_list = Tools::getPagesPost($_SERVER["QUERY_STRING"], $number_page, $page, $total_count);


        $this->view->setVariable("gname", $gname);
        $this->view->setVariable("gmodel", $gmodel);
        $this->view->setVariable("gnum", $gnum);
        $this->view->setVariable("rsp_list", $rsp_list);
        $this->view->setVariable("rsp_page_list", $rsp_page_list);
        $this->view->setVariable("total_count", $total_count);
        $this->view->setVariable("page", $page);
        $this->view->setVariable("number_page", $number_page);
        $this->view->setViewfilepath(MvcReg::$_moduleName . '/views/goods/' . MvcReg::$_actionName . 'View.php');
        $this->view->render();
    }

    public function goodsWeightaddAction() {
        $table = "goods_weight";
        $page = (!empty($this->params['page'])) ? $this->params['page'] : 1;
        $number_page = (!empty($this->params['number_page'])) ? $this->params['number_page'] : Data::$_number_page;
        $this->view->setVariable("number_page", $number_page);
        $this->view->setVariable("page", $page);
        $id = (!empty($this->params['id'])) ? (int) $this->params['id'] : 0;

        if ($id > 0) {
            //抓單筆資料
            $rsp_row = $this->model->getItemById($id, $table);
            $this->view->setVariable("rsp_row", $rsp_row);
        }

        $this->view->setVariable("number_page", $number_page);
        $this->view->setViewfilepath(MvcReg::$_moduleName . '/views/goods/' . MvcReg::$_actionName . 'View.php');
        $this->view->render();
    }

    public function goodsWeightupdateAction() {
        $table = 'goods_weight';
        $page = (!empty($this->params['page'])) ? (int)$this->params['page'] : 1;
        $goods = (!empty($this->params['goods'])) ? $this->params['goods'] : [];
        $id = (!empty($this->params['id'])) ? (int) $this->params['id'] : '';
        if($goods['goods_weight'] > 20){
            $vaild_msg= '商品重量不可大于20 kg';
            Tools::GoToPrev($vaild_msg);
            return;
        }

        if (empty($id)) {
            //確認重覆
            $where = array("AND" => array("=" => array('goods_num' => $goods['goods_num'], 'isdeleted' => 0)));
            $result = $this->model->getItemByOther($where, $table);
            if (count($result) > 0) {
                $vaild_msg= '商品规格已存在';
                Tools::GoToPrev($vaild_msg);
                return;
            } else {
                //新增
                $goods["isdeleted"] = 0;
                $goods['createdt'] = 'now()';
                $r = $this->model->addItem($goods, $table);

                //日志
                $log = array();
                $log['table_id'] = $r;
                $log['table_name'] = $table;
                $log['type'] = 1;
                $log['page'] = "商品重量";
                $log['content'] = $log['page'] . "：" . $goods['goods_num'];
                $org = array();
                $this->model->setLog($log, $org, $goods);
            }
        } else {
            //修改
            $org = $this->model->getItemById($id, $table);
            $goods['updatedt'] = 'now()';
            $where_fields = array("AND" => array("=" => array('id' => $id, "isdeleted" => 0)));
            $r = $this->model->updateItem($goods, $where_fields, $table);

            //日志
            $log = array();
            $log['table_id'] = $id;
            $log['table_name'] = $table;
            $log['type'] = 2;
            $log['page'] = "修改账号";
            $log['content'] = $log['page'] . "：" . $goods['goods_num'];
            $this->model->setLog($log, $org, $goods);
        }

        $url = '?md=' . MvcReg::$_moduleName . '&cl=' . MvcReg::$_controllerName . '&at=' . str_replace('update', '', MvcReg::$_actionName);
        $url .='&page='.$page;
        Tools::AlertTxt($url, $this->getKeyWord('SAVE') . $this->getKeyWord('Msg_Successful'));
    }
}